import os
import signal
import logging

from tornado.httpserver import HTTPServer
import tornado.ioloop
import tornado.web
from tornado.options import options

from handlers import *

class MyStaticFileHandler(tornado.web.StaticFileHandler):
    def set_extra_headers(self, path):
        self.set_header("Cache-control", "no-cache")

is_closing = False

def signal_handler(signum, frame):
    global is_closing
    logging.info('exiting...')
    is_closing = True

def try_exit(): 
    global is_closing
    if is_closing:
        # clean up here
        tornado.ioloop.IOLoop.instance().stop()
        logging.info('exit success')

settings = {
    "static_path": os.path.join(os.path.dirname(__file__), "static"),
    "static_handler_class": MyStaticFileHandler,
    "template_path": os.path.join(os.path.dirname(__file__), "static", "templates"),
    "cookie_secret": "61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
    "login_url": "/login",
    # "xsrf_cookies": True,
    "debug": True,
}

tornado.options.define(name="mysql_host", default="127.0.0.1", type=str)
tornado.options.define(name="mysql_port", default=3306, type=int)
tornado.options.define(name="mysql_user", default="ailab", type=str)
tornado.options.define(name="mysql_pwd", default="111111", type=str)
tornado.options.define(name="mysql_db", default="ailab_db", type=str)

tornado.options.define(name="docker_swarm_manager_ip", default="", type=str)
tornado.options.define(name="docker_api_url", default="", type=str)
tornado.options.define(name="docker_registry_url", default="", type=str)
tornado.options.define(name="jupyter_startup_args", default="", type=str, multiple=True)
tornado.options.define(name="jupyter_workspace_path", default="", type=str)
tornado.options.define(name="jupyter_target_port", default=8888, type=int)

tornado.options.define(name="data_root_dir", default="", type=str)
tornado.options.define(name="data_root_owner", default="", type=str)
tornado.options.define(name="data_template", default="", type=str)
tornado.options.define(name="data_library", default="", type=str)
tornado.options.define(name="data_student", default="", type=str)
tornado.options.define(name="data_student_lab", default="", type=str)
tornado.options.define(name="data_student_virtualenv", default="", type=str)
tornado.options.define(name="data_assignment_download_dir", default="", type=str)

tornado.options.define(name="allowed_customized_file_types", default="", type=str, multiple=True)

tornado.options.parse_command_line()
tornado.options.parse_config_file("./config.py")

if __name__ == "__main__":
    signal.signal(signal.SIGINT, signal_handler)
    all_handlers = []
    all_handlers.extend(default_handlers)
    application = tornado.web.Application(all_handlers, **settings)
    # 允许最大上传200M文件
    http_server = HTTPServer(application, max_buffer_size=209715200, max_body_size=209715200)
    http_server.listen(8888)

    tornado.ioloop.PeriodicCallback(try_exit, 100).start() 
    instance = tornado.ioloop.IOLoop.instance()
    instance.start()
